package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecificationVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@DubboService
@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> listSpecifications(String specName) {
        TbSpecificationExample example = new TbSpecificationExample();
        if (!"".equals(specName)){
            TbSpecificationExample.Criteria criteria = example.createCriteria();
            criteria.andSpecNameLike("%"+specName+"%");
        }
        return tbSpecificationMapper.selectByExample(example);
    }

    @Transactional
    @Override
    public SpecificationVo selectSpecificationAndOptionById(Long id) {
        //根据id查询模板数据
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        //创建模板的vo类,里面定义了模板的所有属性
        SpecificationVo specificationVo = new SpecificationVo();
        BeanUtils.copyProperties(tbSpecification,specificationVo);//使用这个方法将查出来的信息copy到vo类中
        //根据模板id查找option数据
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);
//        将查出来的集合添加到vo中
        specificationVo.setOptions(tbSpecificationOptions);
        return specificationVo;
    }

    @Transactional
    @Override
    public void addSpecificationAndOption(String specName, List<TbSpecificationOption> tbSpecificationOptions) {
        TbSpecification tbSpecification = new TbSpecification();
        tbSpecification.setSpecName(specName);
        tbSpecificationMapper.insertSelective(tbSpecification);
        Long specId = tbSpecification.getId();
        //使用java8特性,stream
        tbSpecificationOptions.stream().forEach((option) ->{
            option.setSpecId(specId);
            tbSpecificationOptionMapper.insertSelective(option);
        });
    }
    @Transactional
    @Override
    public void updateSpecificationAndOption(String specName, List<TbSpecificationOption> tbSpecificationOptions, Long id) {
        TbSpecification tbSpecification = new TbSpecification();
        tbSpecification.setSpecName(specName);
        tbSpecification.setId(id);
        //根据id修改名字.
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);

        //根据specId删除信息
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        tbSpecificationOptionMapper.deleteByExample(example);
        //使用java8特性,stream,再添加
        tbSpecificationOptions.stream().forEach((option) ->{
            option.setSpecId(id);
            tbSpecificationOptionMapper.insertSelective(option);
        });

    }
    @Transactional //事务注解
    @Override
    public void delSpecificatons(List<Long> ids) {
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tbSpecificationMapper.deleteByExample(example);

        TbSpecificationOptionExample example1 = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andSpecIdIn(ids);
        tbSpecificationOptionMapper.deleteByExample(example1);
    }
}
